package xyz.eden.cloud.common.web.spring;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

@Component
public class EnvFactory implements ApplicationContextAware {

    private static ApplicationContext context;

    @Override
    public void setApplicationContext(@NonNull ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    public static <T> T getEnvProperties(String key, Class<T> targetType, T defaultVal) {
        return getEnvironment().getProperty(key, targetType, defaultVal);
    }

    public static <T> T getEnvProperties(String key, Class<T> targetType) {
        return getEnvironment().getProperty(key, targetType);
    }

    public static String getEnvProperties(String key, String defaultVal) {
        return getEnvironment().getProperty(key, defaultVal);
    }

    public static String getEnvProperties(String key) {
        return getEnvironment().getProperty(key);
    }

    public static boolean containsProperty(String key) {
        return getEnvironment().containsProperty(key);
    }

    public static Environment getEnvironment() {
        return context.getEnvironment();
    }
}
